﻿using System;
using System.IO;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;

namespace XwOa
{
    internal class WebHelper
    {
        private static string _cookieHeader;

        public static string PostData(string strUrl, string param)
        {
            WebRequest request = WebRequest.Create(strUrl);
            request.ContentType = "application/x-www-form-urlencoded";
            request.Method = "POST";
            request.Headers.Add("cookie:" + _cookieHeader);

            using (Stream requestStream = request.GetRequestStream())
            {
                using (StreamWriter writer = new StreamWriter(requestStream, Encoding.ASCII))
                {
                    writer.Write(param);
                }
            }

            using (WebResponse response = request.GetResponse())
            {
                using (
                    StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("utf-8")))
                {
                    return reader.ReadToEnd();
                }
            }
        }

        public static string GetValueById(string html, string id)
        {
            Match match = new Regex(string.Format("id=\"{0}\" value=[\\w\\W]+?\"", id)).Match(html);
            return match.Success
                ? match.Value.Replace(string.Format("id=\"{0}\" value=\"", id), "").Replace("\"", "")
                : string.Empty;
        }

        public static string GetValueByName(string html, string id)
        {
            Match match = new Regex(string.Format("name=\"{0}\" value=[\\w\\W]+?\"", id)).Match(html);
            return match.Success
                ? match.Value.Replace(string.Format("name=\"{0}\" value=\"", id), "").Replace("\"", "")
                : string.Empty;
        }

        public static string GetPage(string strUrl)
        {
            WebRequest request = WebRequest.Create(strUrl);
            request.Method = "GET";
            request.Headers.Add("cookie:" + _cookieHeader);

            using (WebResponse response = request.GetResponse())
            {
                using (
                    StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("utf-8")))
                {
                    return reader.ReadToEnd();
                }
            }
        }

        public static string PostLogin(string strUrl, string strArgs)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strUrl);
            request.ContentType = "application/x-www-form-urlencoded";
            request.Method = "POST";
            request.CookieContainer = new CookieContainer();

            using (Stream requestStream = request.GetRequestStream())
            {
                using (StreamWriter writer = new StreamWriter(requestStream, Encoding.ASCII))
                {
                    writer.Write(strArgs);
                }
            }

            using (WebResponse response = request.GetResponse())
            {
                _cookieHeader = request.CookieContainer.GetCookieHeader(new Uri(strUrl));
                using (

                    StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("utf-8")))
                {
                    return reader.ReadToEnd();
                }
            }
        }
    }
}